package com.example.sls.dao.field;

import com.example.sls.pojo.field.FieldUsageStatistics;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 场地使用统计表(FieldUsageStatistics)表数据库访问层
 *
 * @author makejava
 * @since 2024-06-18 10:55:08
 */
public interface FieldUsageStatisticsDao {
  /**
   * 通过ID查询单条数据
   *
   * @param id 主键
   * @return 实例对象
   */
  @Select("select * from field_usage_statistics where id=#{id}")
  FieldUsageStatistics queryById(Integer id);

  /**
   * 查询指定行数据
   *
   * @param fieldUsageStatistics 查询条件
   * @param pageSize             分页对象
   * @param offset               偏移量，起始处
   * @return 对象列表
   */
  @Select("select * from field_usage_statistics where 1=1" +
          " and (id=#{fieldUsageStatistics.id} or (isnull(#{fieldUsageStatistics.id}) or #{fieldUsageStatistics.id}=0))" +
          " and (field_id=#{fieldUsageStatistics.fieldId} or (isnull(#{fieldUsageStatistics.fieldId}) or #{fieldUsageStatistics.fieldId}=0))" +
          " and (usage_date=#{fieldUsageStatistics.usageDate} or (isnull(#{fieldUsageStatistics.usageDate}) or #{fieldUsageStatistics.usageDate}=''))" +
          " and (usage_frequency=#{fieldUsageStatistics.usageFrequency} or (isnull(#{fieldUsageStatistics.usageFrequency}) or #{fieldUsageStatistics.usageFrequency}=0))" +
          " and (created_at=#{fieldUsageStatistics.createdAt} or (isnull(#{fieldUsageStatistics.createdAt}) or #{fieldUsageStatistics.createdAt}=''))" +
          " limit #{pageSize} offset #{offset}")
  List<FieldUsageStatistics> queryAllByLimit(FieldUsageStatistics fieldUsageStatistics, Integer pageSize, Integer offset);

  /**
   * 统计总行数
   *
   * @return 总行数
   */
  @Select("select count(*) from field_usage_statistics")
  long count();

  /**
   * 新增数据
   *
   * @param fieldUsageStatistics 实例对象
   * @return 影响行数
   */
  @Insert("insert into field_usage_statistics (id,field_id,usage_date,usage_frequency,created_at) " +
          "values(" +
          "if(#{id}!=0 and !isnull(#{id}),#{id},null)," +
          "if(#{fieldId}!=0 and !isnull(#{fieldId}),#{fieldId},null)," +
          "if(#{usageDate}!='' and !isnull(#{usageDate}),#{usageDate},null)," +
          "if(#{usageFrequency}!=0 and !isnull(#{usageFrequency}),#{usageFrequency},null)," +
          "if(#{createdAt}!='' and !isnull(#{createdAt}),#{createdAt},null)" +
          ")")
  int insert(FieldUsageStatistics fieldUsageStatistics);

  /**
   * 通过id修改数据
   * id 主键
   *
   * @param fieldUsageStatistics 实例对象
   * @return 影响行数
   */
  @Update("update field_usage_statistics set " +
          "field_id=if(isnull(#{fieldId}) or #{fieldId}=0,field_id,#{fieldId})," +
          "usage_date=if(isnull(#{usageDate}) or #{usageDate}='',usage_date,#{usageDate})," +
          "usage_frequency=if(isnull(#{usageFrequency}) or #{usageFrequency}=0,usage_frequency,#{usageFrequency})," +
          "created_at=if(isnull(#{createdAt}) or #{createdAt}='',created_at,#{createdAt})" +
          " where id=#{id}")
  int update(FieldUsageStatistics fieldUsageStatistics);

  /**
   * 通过主键删除数据
   *
   * @param id 主键
   * @return 影响行数
   */
  @Delete("delete from field_usage_statistics where id=#{id}")
  int deleteById(Integer id);
}

